namespace DotNetNuke.UI.WebControls
{
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class DNNTreeUpLevelWriter : WebControl, IDNNTreeWriter
    {
        private DnnTree _tree;

        protected override void RenderChildren(HtmlTextWriter writer)
        {
            IEnumerator enumerator=null;
            try
            {
                enumerator = this._tree.TreeNodes.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ((TreeNode) enumerator.Current).Render(writer);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    ((IDisposable) enumerator).Dispose();
                }
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "DNNTree");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this._tree.UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this._tree.UniqueID.Replace(":", "_"));
            writer.AddAttribute("sysimgpath", this._tree.SystemImagesPath);
            writer.AddAttribute("indentw", this._tree.IndentWidth.ToString());
            if (this._tree.CheckBoxes)
            {
                writer.AddAttribute("checkboxes", "1");
            }
            if (Strings.Len(this._tree.Target) > 0)
            {
                writer.AddAttribute("target", this._tree.Target);
            }
            if (this._tree.ImageList.Count > 0)
            {
                string expression = "";
                foreach (NodeImage image in this._tree.ImageList)
                {
                    expression = expression + StringType.FromObject(Interaction.IIf(Strings.Len(expression) > 0, ",", "")) + image.ImageUrl;
                }
                writer.AddAttribute("imagelist", expression);
            }
            if (Strings.Len(this._tree.DefaultNodeCssClass) > 0)
            {
                writer.AddAttribute("css", this._tree.DefaultNodeCssClass);
            }
            if (Strings.Len(this._tree.DefaultChildNodeCssClass) > 0)
            {
                writer.AddAttribute("csschild", this._tree.DefaultChildNodeCssClass);
            }
            if (Strings.Len(this._tree.DefaultNodeCssClassOver) > 0)
            {
                writer.AddAttribute("csshover", this._tree.DefaultNodeCssClassOver);
            }
            if (Strings.Len(this._tree.DefaultNodeCssClassSelected) > 0)
            {
                writer.AddAttribute("csssel", this._tree.DefaultNodeCssClassSelected);
            }
            if (Strings.Len(this._tree.DefaultIconCssClass) > 0)
            {
                writer.AddAttribute("cssicon", this._tree.DefaultIconCssClass);
            }
            if (Strings.Len(this._tree.JSFunction) > 0)
            {
                writer.AddAttribute("js", this._tree.JSFunction);
            }
            if (Strings.Len(this._tree.WorkImage) > 0)
            {
                writer.AddAttribute("workimg", this._tree.WorkImage);
            }
            writer.AddAttribute("expimg", this._tree.ExpandedNodeImage);
            writer.AddAttribute("colimg", this._tree.CollapsedNodeImage);
            writer.AddAttribute("postback", ClientAPI.GetPostBackEventReference(this._tree, "[NODEID]" + ClientAPI.COLUMN_DELIMITER + "Click"));
            if (this._tree.PopulateNodesFromClient)
            {
                if (ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.XMLHTTP))
                {
                    writer.AddAttribute("callback", ClientAPI.GetCallbackEventReference(this._tree, "'[NODEXML]'", "this.callBackSuccess", "oTNode", "this.callBackFail", "this.callBackStatus"));
                }
                else
                {
                    writer.AddAttribute("callback", ClientAPI.GetPostBackClientHyperlink(this._tree, "[NODEID]" + ClientAPI.COLUMN_DELIMITER + "OnDemand"));
                }
                if (Strings.Len(this._tree.CallbackStatusFunction) > 0)
                {
                    writer.AddAttribute("callbackSF", this._tree.CallbackStatusFunction);
                }
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
        }

        public void RenderTree(HtmlTextWriter writer, DnnTree tree)
        {
            this._tree = tree;
            this.RenderControl(writer);
        }
    }
}

